<template>
    <base-breadcrumb>
        <div class="about-master-container">
            <a-card v-loading="loading">
                <title-name title="基本信息"></title-name>
                <a-form-model :model="form" ref="formRules" :rules="formRules">
                    <a-row>
                        <a-col span="8">
                            <a-form-model-item label="活动名称" prop="activityName">
                                <b-input v-model="form.activityName" :disabled="disabled" />
                            </a-form-model-item>
                        </a-col>

                        <a-col span="8">
                            <a-form-model-item label="层级" prop="level">
                                <BSelect disabled v-model="form.level" :list="options.level"></BSelect>
                            </a-form-model-item>
                        </a-col>
                        <a-col span="8">
                            <a-form-model-item label="工会组织" prop="orgId">
                                <SelectTreeUnion :is-auth="false" v-model="form.orgId" @getOption="getOption"
                                                 :disabled="disabled"></SelectTreeUnion>
                            </a-form-model-item>
                        </a-col>
                        <a-col span="8">
                            <a-form-model-item label="活动时间" prop="activityTime">
                                <a-date-picker v-model="form.activityTime" valueFormat="x" :disabled="disabled" />
                            </a-form-model-item>
                        </a-col>
                        <a-col span="8">
                            <a-form-model-item label="活动地点" prop="activityAddress">
                                <b-input v-model="form.activityAddress" :disabled="disabled" />
                            </a-form-model-item>
                        </a-col>
                        <a-col span="24">
                            <a-form-model-item label="活动内容" prop="activityContent">
                                <b-input v-model="form.activityContent" :disabled="disabled" />
                            </a-form-model-item>
                        </a-col>
                        <a-col span="8">
                            <a-form-model-item label="附件" prop="fileInfoVos">
                                <b-upload uploadType="single" businessCode="UN02" :fileList.sync="form.fileInfoVos"
                                          :disabled="disabled" />
                                <span v-if="!disabled">支持扩展名：.rar .zip .doc .docx .pdf .jpg...</span>
                            </a-form-model-item>
                        </a-col>
                    </a-row>
                </a-form-model>
            </a-card>
            <div class="tar page-btn-right-top">
                <a-button v-if="!disabled" type="primary" :loading="loading" @click="save" class="mr10p">提交</a-button>
                <a-button @click="$close">关闭</a-button>
            </div>
        </div>
    </base-breadcrumb>
</template>

<script>

import HelpActivitiesLogApi from '@/api/union/employeeCare/HelpActivitiesLog'
import SelectTreeUnion from '@/components/Select/SelectTreeUnion'
import BSelect from '@/components/BComponents/BSelect'

export default {
    name:'a'+Date.now(),
    components: {
        SelectTreeUnion,
        BSelect
    },
    data() {
        return {
            disabled: this.$route.query.detail == 1,
            loading: false,
            form: {
                fileInfoVos: [],
                level: '',
                orgId: '',
                companyId: '',
                branchComId: '',
                projectId: '',
                speakerId: ''
            },
            formRules: {
                activityAddress: { required: true, message: '活动地点不能为空', trigger: 'blur' },
                activityContent: { required: true, message: '活动内容不能为空', trigger: 'change' },
                activityName: { required: true, message: '活动名称不能为空', trigger: 'change' },
                activityTime: { required: true, message: '活动时间不能为空', trigger: 'change' },
                orgId: { required: true, message: '工会组织不能为空', trigger: 'change' }
            },
            options: {
                level: []
            }
        }
    },

    created: async function() {
        if (this.$route.query.id) {
            this.form.id = this.$route.query.id
            await this.getBaseInfo()
        }
        this.getOption()
        this.getTypeLabel({ type: 1 })
    },
    methods: {
        getOption(e) {
            if (!e) return
            let levelId = e.unionLevelId // 层级id
            this.form.level = levelId
            this.form.orgName = e.unionOrgName
            /**
             * 添加表单组织机构参数
             */
            // 公司级
            if (levelId === '605004340528287745') {
                this.form.companyId = e.id
                // 分公司级
            } else if (levelId === '605004401341501446') {
                this.form.branchComId = e.id
                this.form.companyId = e.parentId
                // 项目级
            } else if (levelId === '605004528177254447') {
                this.form.projectId = e.id
                this.form.branchComId = e.parentId
            }
        },
        async getTypeLabel(args) {
            const { unionApiUrl } = require('@/api/config.js')
            const Union = require('@/api/union').default
            const { getLabels } = new Union(unionApiUrl, 'queryTypesLabelList')
            let res = await getLabels(args)
            this.options.level = res.data
        },
        async getBaseInfo() {
            this.loading = true
            let res = await HelpActivitiesLogApi.getSupportRecordById({}, { id: this.form.id })
            if (res.code === this.SUCCESS_CODE) {
                this.form = res.data
                this.form.fileInfoVos = this.form.fileInfoVos ? this.form.fileInfoVos : []
            }
            this.loading = false
        },
        async save() {
            this.$refs.formRules.validate(async valid => {
                if (valid) {
                    this.loading = true
                    let res
                    if (this.form.id) {
                        res = await HelpActivitiesLogApi.updateByPrimaryKeySelective(this.form)
                    } else {
                        res = await HelpActivitiesLogApi.insertSelective(this.form)
                    }
                    if (res.code === 200) {
                        this.$message.success('保存成功')
                        this.form.id = res.data.id
                        await this.getBaseInfo()
                        this.$close()
                    }
                    this.loading = false
                } else {
                    this.$noticeWarning('请填写完整')
                }
            })
        }

    }
}
</script>
